﻿using Aliyun.OSS;
using Newtonsoft.Json.Linq;
using ProductTool.Tools;
using SpiderSharp;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ProductTool.Service
{
    public class HeaderService : SpiderEngine, ISpiderEngine
    {
        public int pn { get; set; } = 0;
        string url = "https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E5%A4%B4%E5%83%8F&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&hd=&latest=&copyright=&word=%E5%A4%B4%E5%83%8F&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&cg=head&pn={0}&rn=30&gsm=3c&1583911566565=";
        int count = 0;
        public HeaderService()
        {
            this.SetUrl(string.Format(url, this.pn));          
        }

        protected override IEnumerable<SpiderContext> OnRun()
        {
            dynamic json = new ExpandoObject();
            //var quotes = this.node.SelectNodes("div.quote");
            var quotes = this.AngleDocument.DocumentElement.GetElementsByTagName("body")[0].InnerHtml.Replace("</strong>",string.Empty);
            var result = JObject.Parse(quotes);

            foreach (var item in result["data"])
            {
                yield return this.Fetch(() =>
                {
                    if (item["thumbURL"]!=null)
                    {
                        ct.Data.ImgUrl = System.Web.HttpUtility.HtmlDecode(item["thumbURL"].ToString());

                        //var url = System.Web.HttpUtility.HtmlDecode(item["thumbURL"].ToString());
                        //HttpClient client = new HttpClient();
                        //var download = client.GetAsync(url).Result;
                        //if (download.StatusCode == System.Net.HttpStatusCode.OK)
                        //{
                        //    ct.Data.ImgUrl = System.Web.HttpUtility.HtmlDecode(item["thumbURL"].ToString());
                        //}                       
                       
                    }                    
                });
            }          
        }

        protected override string FollowPage()
        {
            this.pn= this.pn+30;
            var str=  string.Format(url, this.pn);
            if (this.pn>500)
            {
                str = null;
            }
            return str;
        }
        protected async override Task SuccessPipelineAsync(SpiderContext context)
        {
            count++;
            var pic_path = context.Data.ImgUrl;
            //context.RunPrintToConsolePipeline();
            Console.WriteLine($"{count}===>"+ pic_path);

   
            OssClient client = new OssClient("http://oss-us-west-1.aliyuncs.com", "LTAIetladkJrhOgV", "LAKbjg9gdee2IHRTKZHtAtgKCEli2I");
            var key = Common.MD5Encrypt(context.Data.ImgUrl);
            try
            {
                HttpClient hClient = new HttpClient();
                var stream = await hClient.GetStreamAsync(pic_path);
                var result = client.PutObject("ec-us-west", "avatar/" + key, stream);
                if (!string.IsNullOrEmpty(result.RequestId) && result.HttpStatusCode == System.Net.HttpStatusCode.OK)
                {
                    Console.WriteLine("OSS Success" + count);
                    File.AppendAllText("imgs.txt", pic_path + Environment.NewLine);
                    File.AppendAllText("md5.txt", key + Environment.NewLine);
                }
            }
            catch (Exception)
            {
                
            }
       
           
        }
    

        protected async override Task ErrorPipelineAsync(SpiderContext context)
        {
            context.RunEmbedMetadataPipeline();
            context.RunPrintToConsolePipeline();
        }
    }
}
